package com.example.nowait.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.nowait.R;
import com.example.nowait.utils.StringUtils;



/**
 * Created by sheng on 2016/2/19.
 * 一些提示信息显示，包含有加载过程的显示
 */
public class TipInfoLayout extends FrameLayout {

    private String netWorkError = "网络访问失败\r\n轻触重新加载";
    private String empty = "暂无数据";

    private ProgressBar mPbProgressBar;

    private View mTipContainer;

    private TextView mTvTipMsg;

    private LinearLayout load_layout ;

    private ImageView error_img;

    public TipInfoLayout(Context context) {
        super(context);
        initView(context);
    }

    public TipInfoLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TipInfoLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.tip_info_layout, null, false);
        mPbProgressBar = (ProgressBar) view.findViewById(R.id.pb_loading);
        mTvTipMsg = (TextView) view.findViewById(R.id.tv_tip_msg);
        load_layout = (LinearLayout)view.findViewById(R.id.load_layout);
        error_img = (ImageView)view.findViewById(R.id.error_img);
        mTipContainer = view.findViewById(R.id.ll_tip);
        setLoading();
        addView(view);
    }

    public void setOnClick(OnClickListener onClik) {
        this.setOnClickListener(onClik);
    }

    public void setHiden() {
        this.setVisibility(View.GONE);
    }

    public void setLoading() {
        this.setVisibility(VISIBLE);
        this.load_layout.setVisibility(View.VISIBLE);
        this.mTipContainer.setVisibility(View.GONE);
        this.setClickable(false);
    }

    public void setNetWorkError(){
        String tip = netWorkError;
        this.setVisibility(VISIBLE);
        this.load_layout.setVisibility(View.GONE);
        this.mTipContainer.setVisibility(View.VISIBLE);
        this.error_img.setImageResource(R.mipmap.page_icon_network);
        this.mTvTipMsg.setText(tip);
        this.setClickable(true);
    }

    public void setLoadError(String errorTip) {
        String tip = netWorkError;
        if (errorTip != null && !StringUtils.isEmpty(errorTip))
            tip = errorTip;
        this.load_layout.setVisibility(View.GONE);
        this.mTipContainer.setVisibility(View.VISIBLE);
        this.mTvTipMsg.setText(tip);
        this.setClickable(true);
    }

    public void setEmptyData() {
        this.setVisibility(VISIBLE);
        String tip = empty;
        this.load_layout.setVisibility(View.GONE);
        this.error_img.setImageResource(R.mipmap.page_icon_empty);
        this.mTipContainer.setVisibility(View.VISIBLE);
        this.mTvTipMsg.setText(tip);
        this.setClickable(true);
    }


}
